<?php
include_once("includes/common.php");
include_once("class/User.class.php");
include_once("class/Explore.class.php");
include_once("class/Action.class.php");
require_once("islogin.php");

$pageName = "发现";
$explore = new Explore();
$tags_array = json_decode($explore->randomTags(1),true);

if(isset($_GET['note_type']) && !in_array($_GET['note_type'], [0, 1, 10])){
    return;
}

$current_url = $_SERVER['REQUEST_URI'];
$query_params = $_GET; // 获取当前的 GET 参数
// 图文按钮的 URL
$query_params['note_type'] = 0; // 设置 note_type 为 0
$tuwen_url = strtok($current_url, '?') . '?' . http_build_query($query_params); // 生成新的 URL

// 视频按钮的 URL
$query_params['note_type'] = 1; // 设置 note_type 为 1
$shipin_url = strtok($current_url, '?') . '?' . http_build_query($query_params); // 生成新的 URL

// 视频按钮的 URL
$query_params['note_type'] = 10; // 设置 note_type 为 1
$user_url = strtok($current_url, '?') . '?' . http_build_query($query_params); // 生成新的 URL

// 移除 note_type 参数
unset($query_params['note_type']); // 去除 note_type 参数
$all_url = strtok($current_url, '?') . '?' . http_build_query($query_params); // 生成没有 note_type 的 URL

if (isset($_GET['type'])&&isset($_GET['so'])){
    return;
}

if(isset($_GET['so'])){
    if ($user_info['message']['user_id']){  //登录才记录
        if (strlen($_GET['so']) > 200) {
            return;
        }
        $action = new Action();
        $search_keywords = $_GET['so'];
        $action->userSearch($user_info['message']['user_id'],$search_keywords);
    }
}


?>
<!DOCTYPE html>
<html>
<?php require_once('head.php'); ?>
	<body>
    <?php require_once('header.php'); ?>
		<div class="container-fluid content">
			<div class="row">
                <?php require_once('menu.php'); ?>
				<div class="col watercon" id="waterfall">
					<div class="row">
                        <?php if (isset($_GET['so'])){?>
                            <button class="typeBtn" onclick="window.location.href='<?php echo $all_url; ?>'" <?php if(!isset($_GET['note_type']) || !in_array($_GET['note_type'], [0, 1, 10])){echo 'id="typeActive"';}?>>全部</button>
                            <button class="typeBtn" onclick="window.location.href='<?php echo $tuwen_url; ?>'" <?php if(isset($_GET['note_type']) && $_GET['note_type']==0){echo 'id="typeActive"';}?>>图文</button>
                            <button class="typeBtn" onclick="window.location.href='<?php echo $shipin_url; ?>'" <?php if($_GET['note_type']==1){echo 'id="typeActive"';}?>>视频</button>
                            <button class="typeBtn" onclick="window.location.href='<?php echo $user_url; ?>'" <?php if($_GET['note_type']==10){echo 'id="typeActive"';}?>>用户</button>
                        <?php }else{?>
                        <button class="typeBtn" onclick="window.location.href='/'" <?php if (!$_GET['type']){echo 'id="typeActive"';}?>>推荐</button>
                        <?php for ($tag=0;$tag<count($tags_array);$tag++){?>
                            <button class="typeBtn" <?php if ($_GET['type']==$tags_array[$tag]['tag']){echo 'id="typeActive"';}?> onclick="window.location.href='/?type=<?php echo $tags_array[$tag]['tag'];?>'"><?php echo $tags_array[$tag]['tag'];?></button>
                        <?php }?>
                        <?php }?>
					</div>

                    <?php if($_GET['note_type']==10){?>
                        <div class="row userso" id="usersoContainer">

                            <!--souser条每行start-->
                            <!--<div class="row" onclick="window.open('/profile.php?id=', '_blank')">
                                <div class="col-1 so_userimg">
                                    <img src="/data/avatar/default.jpg" alt="" class="notify_uavatar souser_avatar"/>
                                </div>
                                <div class="col">
                                    <div class="n_uname">Nickname</div>
                                    <div class="so_ublueid">小蓝书号：777326309</div>
                                    <div class="so_desc">粉丝・4 <span class="divider"></span> 笔记・198</div>
                                </div>
                                <div class="col-2">
                                    <div class="col text-end follow_btn" style="padding-top: 10px">
                                        <button id="followButton" data-fans-id="" data-uid="">关注</button>
                                    </div>
                                </div>
                            </div>-->
                            <!--souser条每行end-->
                        </div>
                    <?php }else{?>
                        <!--瀑布流start-->
                        <div class="row water" id="notesContainer">


                        </div>
                        <!--瀑布流end-->
                    <?php }?>
                    <!--无内容显示-->
                    <div class="row" id="noNote">
                        <div class="nulltips notify_end">- THE END -</div>
                    </div>
                    <!--无内容end-->
				</div>
			</div>
		</div>
		<div class="flash_btn" onclick="location.reload();">
			<img src="/static/img/flash.svg" alt="" />
		</div>
        <div class="flash_btn_text" id="flash_text">刷新</div>
        <div class="up_btn">
            <img src="/static/img/up.svg" alt="" />
        </div>
        <div class="up_btn_text" id="up_text">回到顶部</div>
        <script>
            let uid = '<?php echo $user_info['message']['user_id'];?>';
            <?php if (isset($_GET['so'])||isset($_GET['type'])){?>
            let apiType = 'search';
            <?php }else{?>
                <?php if ($islogin==1){?>
                    let apiType = 'prandom';
                <?php }else{?>
                    let apiType = 'random';
                <?php }?>
            <?php }?>
            let page = 1;
            <?php
            $type = $_GET['type'] ?? ($_GET['so'] ?? '');

            $keyword = htmlspecialchars(trim($type), ENT_QUOTES, 'UTF-8');
            ?>
            let keyword = '<?php echo $keyword;?>';
            let note_type = <?php echo $_GET['note_type'] ?? 3;?>;
        </script>
        <script src="/static/js/jquery.min.js"></script>
        <script src="/static/js/message.min.js"></script>
        <script src="/static/js/custom.js"></script>
        <script src="/static/js/masonry.pkgd.min.js"></script>
        <script src="/static/js/imagesloaded.pkgd.min.js"></script>
        <?php if ($_GET['note_type']==10){?>
            <script src="/static/js/search-user.js"></script>
        <?php }else{?>
            <script src="/static/js/exp-api.js"></script>
            <script>
                document.addEventListener('DOMContentLoaded', function() {
                    loadRandomNotes(msnry);
                });
            </script>
        <?php }?>


	</body>
</html>